package com.vf.admin.client.dao;

import com.vf.admin.common.db.BaseDao;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppMingXiSongLi;
import com.vf.admin.miaodong.entity.AppUserGift;
import com.vf.admin.utils.StringEx;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * @author anxiang
 * @version 1.0.0
 * @ClassName AppMingXiSongLiDao.java
 * @Description TODO
 * @createTime 2021年11月02日 15:22:00
 */
@Repository
public class AppUserGiftDao extends BaseDao<AppUserGift> {

    public void saveOrUpdate(AppUserGift pojo) {
        if (pojo.getId() == null || pojo.getId() == 0) {
            pojo.setId(this.add(pojo));
        } else {
            this.update(pojo);
        }
    }

    /**
     * 更新礼物信息
     */
    public void uodateData(Integer giftid,String picture,String giftname) {
        if(StringEx.isNull(giftid)){
            return ;
        }
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuffer sql =new StringBuffer(" update app_user_gift set picture=? ") ;
        paramList.add(picture);
        sql.append(" ,giftname=? ");
        paramList.add(giftname);
        sql.append(" where giftid=? ");
        paramList.add(giftid);
        this.update(sql.toString(),paramList);
    }

    //查找用户送礼统计
    public List<AppUserGift> findList(Integer userid) {
        if (StringEx.isNull(userid)) {
            return null;
        }
        StringBuilder str = new StringBuilder();
        List<Object> list = new ArrayList<>();
        str.append("select * from app_user_gift where 1=1 ");
        str.append(" and userid = ? ");
        list.add(userid);
        String sql = str.toString();
        return this.queryList(sql, list, AppUserGift.class, null);
    }


    //查找用户送礼统计
    public AppUserGift getGift(Integer userid,Integer giftid) {
        if (StringEx.isNull(userid)) {
            return null;
        }
        StringBuilder str = new StringBuilder();
        List<Object> list = new ArrayList<>();
        str.append("select * from app_user_gift where 1=1");
        str.append(" and userid = ? ");
        list.add(userid);
        str.append(" and giftid = ? ");
        list.add(giftid);
        String sql = str.toString();
        return this.queryObj(sql, list, AppUserGift.class);
    }


    /*
     * @Description 更新用户金币
     * @Param: [id, jinbi]
     * @return: void
     * @Author: ljx
     * @Date: 2021/07/22
     */
    public void updateGiftNum(Integer userid,Integer giftid,Integer giftnum) {
        if (StringEx.isNull(userid)) {
            return;
        }
        if (StringEx.isNull(giftid)) {
            return;
        }
        if (StringEx.isNull(giftnum)) {
            return;
        }
        String sql = " UPDATE app_user_gift SET giftnum = giftnum+ ?  WHERE userid = ? and giftid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(giftnum);
        paramList.add(userid);
        paramList.add(giftid);
        this.update(sql, paramList);
    }
}